﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
using LinkCard.AdminCP.Common;
using System.Data;
namespace LinkCard.AdminCP.Adv
{
    public partial class ad_new : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }

            if (!Page.IsPostBack)
            {
                var dt = new DataTable();
                dt = DataHelper.SelectTable("Select count(*) as count from ads",false);
                int nCount = 1;
                if (dt.Rows.Count > 0) nCount = ConvertUtility.ToInt32(dt.Rows[0]["count"]) + 1;
                for (int i = 0; i < nCount; i++)
                {
                    AdPosition.Items.Insert(i, new ListItem((i + 1).ToString(), (i + 1).ToString()));
                }
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            add();
            Response.Redirect("/Adv/ad_list.aspx");
        }

        protected void add()
        {
            var dao = new AdDao();
            var item = new LinkCard.Model.Ads();

            item.AdTitle = AdTitle.Value;
            item.AdFilePath = AdFilePath.Value;
            item.AdPosition = ConvertUtility.ToInt32( AdPosition.SelectedValue);
            item.AdPostDate = ConvertUtility.ToDateTime(AdPostDate.Value);
            item.AdLink = AdLink.Value;
            item.isActive = isActive.Checked;
            dao.Add(item);

        }

        
    }
}
